package com.sooth.common.encrypt;

import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.SecureUtil;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * AES算法实现
 */
@Service
public class AesEncryptor implements IEncryptor {

    byte[] secretKey = "dream-orm::sooth".getBytes();

    /**
     * 加密
     *
     * @param value 待加密字符串
     */
    @Override
    public String encrypt(String value) {
        return SecureUtil.aes(secretKey).encryptHex(value, StandardCharsets.UTF_8);
    }

    /**
     * 解密
     *
     * @param value 待加密字符串
     */
    @Override
    public String decrypt(String value) {
        try {
            return SecureUtil.aes(secretKey).decryptStr(value, StandardCharsets.UTF_8);
        } catch (CryptoException e) {
            return value;
        }
    }
}
